<?php

class SkillController extends Controller
{
	public function actionIndex()
	{
		$uid = Yii::app()->request->getParam('uid');
		if (!$uid)
			throw  new CHttpException(404,'Param is not enough');
		$userSkillForm = new UserSkill();
		
		
		$langSkill    = $this->getData('language');
		$softSkill    = $this->getData('soft skill');
		$programSkill = $this->getDataProgram();
		$this->render('index',array(
			'userSkillForm'=>$userSkillForm,
			'langSkill'=>$langSkill,
			'softSkill'=>$softSkill,
			'programSkill'=>$programSkill,
		));
	}
	
	public function actionAddSkill() {
		$uid = Yii::app()->request->getParam('uid');
		if (!$uid)
			throw  new CHttpException(404,'Param is not enough');
			
		$userSkillForm = new UserSkill();
		$stype         = Yii::app()->request->getParam('stype');
		$userSkillForm->attributes = $_POST['UserSkill'];
		
		$userSkillForm->user_skill_user_id = $uid;				
		
		if (!$userSkillForm->save())
			throw new CHttpException('404','Error create new language skill');
		
		if ($stype == 'program')
			$data = $this->getDataProgram();
		else 
			$data = $this->getData($stype);
		$this->renderPartial('_skill',array('langSkill'=>$data,'stype'=>$stype),false,true);
	}
	
	public function actionDelSkill() {
		$sid = Yii::app()->request->getParam('sid');
		$stype         = Yii::app()->request->getParam('stype');
		$userSkill = UserSkill::model()->findByPk($sid);
		
		if (!$userSkill or !$userSkill->delete())
			throw new CHttpException('404','Error del skill');
		
		if ($stype == 'program')
			$data = $this->getDataProgram();
		else 
			$data = $this->getData($stype);
		$this->renderPartial('_skill',array('langSkill'=>$data,'stype'=>$stype),false,true);
	}
	
	public function getData($parentCategory) {
		//get new data
		$uid = Yii::app()->request->getParam('uid');
		if (!$uid)
			throw  new CHttpException(404,'Param is not enough');
			
		$skillList = '('.implode(',',Skill::listSkillId($parentCategory)).')'; 
		$langSkill=new CActiveDataProvider('UserSkill',array(
			'criteria'=>array(
			 	'condition'=>"user_skill_user_id =:user_skill_user_id and user_skill_skill_id in $skillList",
				'params'=>array(
					'user_skill_user_id'=>$uid,
				)
			),
		));	
		return $langSkill;
	}
	
	public function getDataProgram() {
		//get new data
		$uid = Yii::app()->request->getParam('uid');
		if (!$uid)
			throw  new CHttpException(404,'Param is not enough');
			
		$skillList = '('.implode(',',Skill::listSkillProgramId()).')';
		$langSkill=new CActiveDataProvider('UserSkill',array(
			'criteria'=>array(
			 	'condition'=>"user_skill_user_id =:user_skill_user_id and user_skill_skill_id in $skillList",
				'params'=>array(
					'user_skill_user_id'=>$uid,
				)
			),
		));	
		return $langSkill;
	}
}